package com.ruoyi.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.base.domain.bo.BsProjectBo;
import com.ruoyi.base.domain.vo.BsProjectVo;
import com.ruoyi.base.domain.BsProject;
import com.ruoyi.base.mapper.BsProjectMapper;
import com.ruoyi.base.service.IBsProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目管理Service业务层处理
 *
 * @author zyb
 * @date 2024-07-16
 */
@RequiredArgsConstructor
@Service
public class BsProjectServiceImpl implements IBsProjectService {

    private final BsProjectMapper baseMapper;

    /**
     * 查询项目管理
     */
    @Override
    public BsProjectVo queryById(Long projectId){
        return baseMapper.selectVoById(projectId);
    }

    /**
     * 查询项目管理列表
     */
    @Override
    public TableDataInfo<BsProjectVo> queryPageList(BsProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsProject> lqw = buildQueryWrapper(bo);
        Page<BsProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目管理列表
     */
    @Override
    public List<BsProjectVo> queryList(BsProjectBo bo) {
        LambdaQueryWrapper<BsProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsProject> buildQueryWrapper(BsProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsProject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), BsProject::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectInfo()), BsProject::getProjectInfo, bo.getProjectInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectStatus()), BsProject::getProjectStatus, bo.getProjectStatus());
        return lqw;
    }

    /**
     * 新增项目管理
     */
    @Override
    public Boolean insertByBo(BsProjectBo bo) {
        BsProject add = BeanUtil.toBean(bo, BsProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改项目管理
     */
    @Override
    public Boolean updateByBo(BsProjectBo bo) {
        BsProject update = BeanUtil.toBean(bo, BsProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
